package use_case.processRental;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class AdminPanel extends JDialog implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7897596825283051692L;

	private Ledger ledger;
	
	AdminPanel( JFrame parent, Ledger ledger )
	{
		super( parent, "Admin Control Panel", true );
		
		this.ledger = ledger;
		
		JPanel messagePane = new JPanel();
	    messagePane.add(new JLabel("Please Choose An Action"));
	    getContentPane().add(messagePane);
	    
	    JPanel buttonPane = new JPanel();
	    GridLayout gridLayout = new GridLayout();
	    gridLayout.setColumns(1);
	    gridLayout.setRows(5);
	    buttonPane.setLayout(gridLayout);
	    JButton button = new JButton("Print Sale Information"); 
	    buttonPane.add(button); 
	    button.addActionListener(this);
	    button = new JButton("Print Stats by Movie"); 
	    buttonPane.add(button); 
	    button.addActionListener(this);
	    button = new JButton("Print Stats by Genre"); 
	    buttonPane.add(button); 
	    button.addActionListener(this);
	    button = new JButton("Print System Health Log"); 
	    buttonPane.add(button); 
	    button.addActionListener(this);
	    button = new JButton("OK"); 
	    buttonPane.add(button); 
	    button.addActionListener(this);
	    getContentPane().add(buttonPane, BorderLayout.SOUTH);
	    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	    pack(); 
	   }

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton source = (JButton)e.getSource();

		if( source.getText().equals("OK"))
		{
			setVisible(false); 
		    dispose(); 
		}
		else if( source.getText().equals( "Print Sale Information" ))
		{
			String[] list = ledger.getSales();
			
			try{
				  // Create file 
				  FileWriter fstream = new FileWriter("sale stats.txt");
				  BufferedWriter out = new BufferedWriter(fstream);
				  out.write("Customer" +"\t"+ "Amount" + "\t"+"Card Number"+ "\t"+ "Date");
				  out.newLine();
				  
				  for(int i = 0; i < ledger.getNumberOfSales(); i++)
				  {
					  out.write(list[i]);
					  out.newLine();
				  }
				  //Close the output stream
				  out.close();
				  }catch (Exception exception){//Catch exception if any
				  System.err.println("Error: " + exception.getMessage());
				  
			  }
		}
		else if( source.getText().equals( "Print Stats by Movie"))
		{
			RentalStatistics rentalStatistics = ledger.getRentalStatistics();
			
			try{
				  // Create file 
				  FileWriter fstream = new FileWriter("movie stats.txt");
				  BufferedWriter out = new BufferedWriter(fstream);
				  out.write("Movie Name\tSales Count");
				  out.newLine();
				  rentalStatistics.printStatsByName(out);
				  //Close the output stream
				  out.close();
				  }catch (Exception exception){//Catch exception if any
				  System.err.println("Error: " + exception.getMessage());
				  
			  }
		}
		else if( source.getText().equals( "Print Stats by Genre"))
		{
			RentalStatistics rentalStatistics = ledger.getRentalStatistics();
			
			try{
				  // Create file 
				  FileWriter fstream = new FileWriter("genre stats.txt");
				  BufferedWriter out = new BufferedWriter(fstream);
				  out.write("Genre\tSales Count");
				  out.newLine();
				  rentalStatistics.printStatsByGenre(out);
				  //Close the output stream
				  out.close();
				  }catch (Exception exception){//Catch exception if any
				  System.err.println("Error: " + exception.getMessage());
				  
			  }
		}
		else if( source.getText().equals( "Print System Health Log"))
		{
			try{
				  // Create file 
				  FileWriter fstream = new FileWriter("health monitor.txt");
				  BufferedWriter out = new BufferedWriter(fstream);
				  out.write("Time\tMessage");
				  out.newLine();
				  SystemHealthLog.printEvents(out);
				  //Close the output stream
				  out.close();
				  }catch (Exception exception){//Catch exception if any
				  System.err.println("Error: " + exception.getMessage());
				  
			  }
		}
	}
}
